#include "NetworkEvents.h"

//Writes the event to a bitstream and sends it over the network
void CClientEvent::send()
{
    
    RakNet::BitStream bsEvent = this->encode();         //Encode the event into a bitstream
    
    CGame::getInstance()        //Send the bitstream over the network to the server, using the previously set priority and reliability
        ->getMultiplayer()->getClient()->Send(&bsEvent, m_iPriority, m_iReliability, 0);
    
}

 //Writes the event to a bitstream and sends it over the network
void CServerEvent::send(PlayerID pidPlayer)
{
    
    //This makes it send only to that player specified if one is specified
    bool bBroadcast = (pidPlayer == UNASSIGNED_PLAYER_ID);
    
    RakNet::BitStream bsEvent = this->encode();         //Encode the event into a bitstream
    CGame::getInstance()        //Send the bitstream over the network to all clients, using the previously set priority and reliability
        ->getMultiplayer()->getServer()->Send(&bsEvent, m_iPriority, m_iReliability, 0, pidPlayer, bBroadcast);
}

/*
void CCreateObjectEvent::onActivate()
{
    std::cout << "Something" << std::endl;
    std::cout << CGame::getInstance()->getMultiplayer() << std::endl;
    m_pObject = CGame::getInstance()->getMultiplayer()->addNetworkObject(m_sType);
    std::cout << "New object " << m_pObject << std::endl;
    CGame::getInstance()->addGameObject(m_pObject);
    
    m_nidObjectID = m_pObject->getNetIDGenerator().GetNetworkID();
    
}

void CCreateObjectEvent::receive(RakNet::BitStream& bsData)
{
            
    unsigned short iTypeLength;
    bsData.Read(iTypeLength);
    char* cpType = new char[iTypeLength];
    bsData.Read(cpType, iTypeLength);
    m_sType = cpType;
    
    //Read the object's network ID
    bsData.Read(m_nidObjectID);
    
}

void CCreateObjectEvent::onRespond()
{
    
    //Add the object to the network world and the game world
    m_pObject = CGame::getInstance()->getMultiplayer()->addNetworkObject(m_sType);
    CGame::getInstance()->addGameObject(m_pObject);
    
    //Set the object's network ID
    m_pObject->getNetIDGenerator().SetNetworkID(m_nidObjectID);
    
    std::cout << "Client - receive - m_nidObjectID = " << m_nidObjectID.localSystemId << std::endl;
    std::cout << "Client - receive - Object's new set m_nidObjectID = " << m_pObject->getNetIDGenerator().GetNetworkID().localSystemId << std::endl;
    
}*/
